

class Test extends Avalon.VM{
    $id='Test';

    //考试安排中的查询
    exampapers={};
    ksName="";
    ksNum="";
    searchTest=function () {
        //TODO 数据验证
        let searchInput={};
        if(TestVM.ksName=="" && TestVM.ksNum==""){
            searchInput={};
        }else if(TestVM.ksName==""){
            searchInput={TBatch:TestVM.ksNum};
        }else if(TestVM.ksNum==""){
            searchInput={SJName:TestVM.ksName};
        }else{
            searchInput = {SJName:TestVM.ksName,TBatch:TestVM.ksNum}
        }
        Castle.Request.post(Config.getRequestUrl('Exampaper','Search'),{W:searchInput},function (data) {
            for (let i=0;i<data.L.length;i++){
                let temp =  data.L[i].FID;
                let temp2 = data.L[i].KID;
                Castle.Request.post(Config.getRequestUrl('Plan','search'),{W:{FID:data.L[i].FID}},function (data1) {
                    data.L[i].FID = data1.L[0].Name;
                    for(let j = i+1;j<data.L.length;j++){
                        if(data.L[j].FID == temp){
                            data.L[j].FID = data.L[i].FID;
                        }
                    }
                },function (err) {
                    // alert(err );
                });
                Castle.Request.post(Config.getRequestUrl('Discipline','search'),{W:{KID:data.L[i].KID}},function (data1) {
                    data.L[i].KID = data1.L[0].Name;
                    for(let j = i+1;j<data.L.length;j++){
                        if(data.L[j].KID == temp2){
                            data.L[j].KID = data.L[i].KID;
                        }
                    }
                },function (err) {
                    alert(err );
                });
            }
            TestVM.exampapers = data.L;
            console.log(TestVM.exampapers);
        },function (err) {
            console.log(err);

        })
    };

    //根据科目查询试卷模板
    seKemu='';
    seKeMuTest = function () {
        let searchData = {};
        // if(UserVM.searchLayer==""){
        Castle.Request.post(Config.getRequestUrl('Discipline','search'),{W:{Name:TestVM.seKemu}},function (data2) {
            searchData = {KID: data2.L[0].KID};
            Castle.Request.post(Config.getRequestUrl('Exampaper','Search'),{W:searchData},function (data) {
                for (let i=0;i<data.L.length;i++){
                    let temp =  data.L[i].FID;
                    let temp2 = data.L[i].KID;
                    Castle.Request.post(Config.getRequestUrl('Plan','search'),{W:{FID:data.L[i].FID}},function (data1) {
                        data.L[i].FID = data1.L[0].Name;
                        for(let j = i+1;j<data.L.length;j++){
                            if(data.L[j].FID == temp){
                                data.L[j].FID = data.L[i].FID;
                            }
                        }
                    },function (err) {
                        // alert(err );
                    });
                    Castle.Request.post(Config.getRequestUrl('Discipline','search'),{W:{KID:data.L[i].KID}},function (data1) {
                        data.L[i].KID = data1.L[0].Name;
                        for(let j = i+1;j<data.L.length;j++){
                            if(data.L[j].KID == temp2){
                                data.L[j].KID = data.L[i].KID;
                            }
                        }
                    },function (err) {
                        alert(err );
                    });
                }
                TestVM.exampapers = data.L;
                console.log(TestVM.exampapers);
            },function (err) {
                console.log(err);

            })
        },function (err) {
            console.log(err);
        });
    };

    //根据方案查询试卷模板
    seMethod='';
    seMethodTest = function () {
        let searchData = {};
        // if(UserVM.searchLayer==""){
        Castle.Request.post(Config.getRequestUrl('Plan','search'),{W:{Name:TestVM.seMethod}},function (data2) {
            searchData = {FID: data2.L[0].FID};
            Castle.Request.post(Config.getRequestUrl('Exampaper','Search'),{W:searchData},function (data) {
                for (let i=0;i<data.L.length;i++){
                    let temp =  data.L[i].FID;
                    let temp2 = data.L[i].KID;
                    Castle.Request.post(Config.getRequestUrl('Plan','search'),{W:{FID:data.L[i].FID}},function (data1) {
                        data.L[i].FID = data1.L[0].Name;
                        for(let j = i+1;j<data.L.length;j++){
                            if(data.L[j].FID == temp){
                                data.L[j].FID = data.L[i].FID;
                            }
                        }
                    },function (err) {
                        // alert(err );
                    });
                    Castle.Request.post(Config.getRequestUrl('Discipline','search'),{W:{KID:data.L[i].KID}},function (data1) {
                        data.L[i].KID = data1.L[0].Name;
                        for(let j = i+1;j<data.L.length;j++){
                            if(data.L[j].KID == temp2){
                                data.L[j].KID = data.L[i].KID;
                            }
                        }
                    },function (err) {
                        alert(err );
                    });
                }
                TestVM.exampapers = data.L;
                console.log(TestVM.exampapers);
            },function (err) {
                console.log(err);

            })
        },function (err) {
            console.log(err);
        });
    };
    //根据难度查询试卷模板
    seDiffi='';
    seDiffiTest = function () {
        let searchData = {};
        // if(UserVM.searchLayer==""){
        Castle.Request.post(Config.getRequestUrl('Titlelevel','search'),{W:{Level:TestVM.seDiffi}},function (data2) {
            searchData = {ELID: data2.L[0].TLID};
            Castle.Request.post(Config.getRequestUrl('Exampaper','Search'),{W:searchData},function (data) {
                for (let i=0;i<data.L.length;i++){
                    let temp =  data.L[i].FID;
                    let temp2 = data.L[i].KID;
                    Castle.Request.post(Config.getRequestUrl('Plan','search'),{W:{FID:data.L[i].FID}},function (data1) {
                        data.L[i].FID = data1.L[0].Name;
                        for(let j = i+1;j<data.L.length;j++){
                            if(data.L[j].FID == temp){
                                data.L[j].FID = data.L[i].FID;
                            }
                        }
                    },function (err) {
                        // alert(err );
                    });
                    Castle.Request.post(Config.getRequestUrl('Discipline','search'),{W:{KID:data.L[i].KID}},function (data1) {
                        data.L[i].KID = data1.L[0].Name;
                        for(let j = i+1;j<data.L.length;j++){
                            if(data.L[j].KID == temp2){
                                data.L[j].KID = data.L[i].KID;
                            }
                        }
                    },function (err) {
                        alert(err );
                    });
                }
                TestVM.exampapers = data.L;
                console.log(TestVM.exampapers);
            },function (err) {
                console.log(err);

            })
        },function (err) {
            console.log(err);
        });
    };


    //试卷管理中查询真实考试记录
    realhistory={};
    realHistoryfn=function () {
        Castle.Request.post(Config.getRequestUrl('Realexam','search'),{},function (data) {
            console.log(data);
            TestVM.realhistory = data;
        },function (err) {
            console.log(err);

        })
    };


//查询成绩
    scoreData={};
    scoreName="";
    scoreID="";
    scoreSearch = function () {
        let scoreInput={};
        if(TestVM.scoreName==""&&TestVM.scoreID=="") scoreInput={};
        else if(TestVM.scoreName=="") scoreInput={NID:TestVM.scoreID};
        else if(TestVM.scoreID=="") scoreInput = {UserName:TestVM.scoreName};
        else scoreInput={UserName:TestVM.scoreName,NID:TestVM.scoreID};
        Castle.Request.post(Config.getRequestUrl('Score','mSearch'),scoreInput,function (data) {
            console.log(data);
            TestVM.scoreData = data;
        },function (err) {
            console.log(err);
            TestVM.scoreData = {};
            alert(err);

        })
    };

    //添加方案
    waysName="";
    // waysSub="";
    waysDXNum="";
    waysDXScore="";
    waysMDXNum="";
    waysMDXScore="";
    waysPDNum="";
    waysPDScore="";
    waysLevel="";
    waysTime="";
    waysAllScore="";
    addPlan=function () {
        let waysData={Name:TestVM.waysName,DXnumber:TestVM.waysDXNum,DXscore:TestVM.waysDXScore,
            DDXnumber:TestVM.waysMDXNum, DDXscore:TestVM.waysMDXScore,PDnumber:TestVM.waysPDNum,
            PDscore:TestVM.waysPDScore, Score:TestVM.waysAllScore};
        if (TestVM.waysName=="") alert("方案名不能为空");
        else if(TestVM.waysDXNum=="") waysData.DXnumber = 0;
        else if(TestVM.waysDXScore=="") waysData.DXscore = 0;
        else if(TestVM.waysMDXNum=="") waysData.DDXnumber = 0;
        else if(TestVM.waysMDXScore=="") waysData.DDXscore = 0;
        else if(TestVM.waysPDNum=="") waysData.PDnumber = 0;
        else if(TestVM.waysPDScore=="") waysData.PDscore = 0;
        // let dataPlan={Name:"方案一",XZnumber:2,XZscore:5};
        Castle.Request.post(Config.getRequestUrl('Plan','add'),waysData,function (data) {
            console.log(data);
            alert("添加成功");
        },function (err) {
            console.log(err);


        })
    };

    //编辑方案
    editwaysID="";
    editwaysName="";
    // waysSub="";
    editwaysDXNum="";
    editwaysDXScore="";
    editwaysMDXNum="";
    editwaysMDXScore="";
    editwaysPDNum="";
    editwaysPDScore="";
    // editwaysLevel="";
    // editwaysTime="";
    editwaysAllScore="";

    //添加试卷
    addKSName="";
    addKSBatch="";
    addKSSubject="";
    addKSMethod="";
    addKSDifficuty="";
    addKSTime="";
    addKSExamStart="";
    addKSExamEnd="";
    addKSTrainStart="";
    addKSTrainEnd="";
    subject="";
    method="";
    addExam=function () {
        // let subject;
        Castle.Request.post(Config.getRequestUrl('Discipline','search'),{W:{Name:TestVM.addKSSubject}},function (data) {
            TestVM.subject = data.L[0].KID;
            // console.log(TestVM.subject);
        },function (err) {
        });
        // let method;
        Castle.Request.post(Config.getRequestUrl('Plan','search'),{W:{Name:TestVM.addKSMethod}},function (data) {
            TestVM.method = data.L[0].FID;
            // console.log(TestVM.method);
        },function (err) {
        });
        // = TranslateToMthodName(TestVM.addKSMethod);
        let addExamData={KID:TestVM.subject,UID:2,TTime:TestVM.addKSTime,SJName:TestVM.addKSName,TBatch:TestVM.addKSBatch,
            FID:TestVM.method,ELID:TestVM.addKSDifficuty,TSTime:TestVM.addKSExamStart,TETime:TestVM.addKSExamEnd,
            PSTime:TestVM.addKSTrainStart,PETime:TestVM.addKSTrainEnd};
        // Castle.Request.post(Config.getRequestUrl('Exampaper','add'),{KID:2,UID:2,TTime:120,
        //     SJName:'语文上半年考试',TBatch:'001',FID:2,TSTime:2017-8-27,TETime:2017-8-29,
        //     PSTime:2017-8-27,PETime:2017-8-29},function (data) {
        Castle.Request.post(Config.getRequestUrl('Exampaper','add'),addExamData,function (data) {
            // console.log(data);
            alert("添加成功");
        },function (err) {
            // console.log(err);
            alert(err);
        });
    };

    //编辑试卷模板
    editaddSID="";
    editaddKSName="";
    editaddKSBatch="";
    editaddKSSubject="";
    editaddKSMethod="";
    editaddKSDifficuty="";
    editaddKSTime="";
    editaddKSExamStart="";
    editaddKSExamEnd="";
    editaddKSTrainStart="";
    editaddKSTrainEnd="";

    //随机生成试卷
    SID;
    randSample="";//绑定随机生成时的试卷模板
    sjnum="";//绑定生成数量
    createExamRadfn = function () {
        Castle.Request.post(Config.getRequestUrl('Exampaper','search'),{W:{SJName:TestVM.randSample}},function (data) {
            TestVM.SID = data.L[0].SID;
            console.log(TestVM.sjnum);
            Castle.Request.post(Config.getRequestUrl('Realexam','creatEXdb'),{UID:2,SID:TestVM.SID,count:TestVM.sjnum,
            },function (data1) {
                console.log(data1);
                alert("试卷生成成功");
            },function (err) {
                // console.log(err);
                alert(err);
            });
        },function (err) {
            console.log(err);
        });

    };

    //自定义生成试卷中查询题库中的题目列表
    creatlayer="";
    creatmajor="";
    creatlevel="";//难度
    creattype="";//类型
    creatsubject="";//科目
    creatquestion={};
    creatquestionfn=function () {
        Castle.Request.post(Config.getRequestUrl('Discipline','search'),{W:{Name:TestVM.creatsubject}},function (data1) {
            console.log(data1.L[0].KID);
            Castle.Request.post(Config.getRequestUrl('Titletype','search'),{W:{Name:TestVM.creattype}},function (data2) {
                console.log(data2.L[0].TTID);
                Castle.Request.post(Config.getRequestUrl('Question','search'),{W:{KID:data1.L[0].KID,TLID:TestVM.creatlevel,TTID:data2.L[0].TTID}},function (data) {
                    console.log(data);
                    TestVM.creatquestion = data.L;
                    // TestVM.layer = data.L;
                },function (err) {
                    console.log(err);
                })
            },function (err) {
                console.log(err);
            });
            // TestVM.layer = data.L;
        },function (err) {
            console.log(err);
        });
    };


    //查询数据库中所有层次
    layer={};
    layerfn=function () {
        Castle.Request.post(Config.getRequestUrl('Level','search'),{},function (data) {
            // console.log(data.L);
            TestVM.layer = data.L;
        },function (err) {
            console.log(err);
        })
    };
    //查询数据库中所有科目
    subjct={};
    subjctfn=function () {
        Castle.Request.post(Config.getRequestUrl('Discipline','search'),{},function (data) {
            // console.log(data.L);
            TestVM.subjct = data.L;
        },function (err) {
            console.log(err);
        })
    };
//查询数据库中所有专业
    major={};
    majorfn=function () {
        // let majorall={}
        Castle.Request.post(Config.getRequestUrl('Major','search'),{},function (data) {
            // console.log(data.L);
            TestVM.major = data.L;
        },function (err) {
            console.log(err);
        })
    };
    //查询数据库中所有方案
    waysall={};
    waysallfn=function () {
        Castle.Request.post(Config.getRequestUrl('Plan','search'),{},function (data) {
            // console.log(data.L);
            TestVM.waysall = data.L;
        },function (err) {
            console.log(err);
        })
    };
//查询数据了中所有难度
    difficulty={};
    difficultyfn=function () {
        Castle.Request.post(Config.getRequestUrl('Titlelevel','search'),{},function (data) {
            // console.log(data.L);
            TestVM.difficulty = data.L;
        },function (err) {
            console.log(err);
        })
    };
    //查询数据库中所有真实试卷
    realExam={};
    realExamfn = function () {
        Castle.Request.post(Config.getRequestUrl('Realexam','search'),{},function (data) {
            // console.log(data.L);
            TestVM.realExam = data.L;
        },function (err) {
            console.log(err);
        })
    };
//查询所有试卷模板
    testSample={};
    testSamplefn = function () {
        Castle.Request.post(Config.getRequestUrl('Exampaper','search'),{},function (data) {
            // console.log(data.L);
            TestVM.testSample = data.L;
        },function (err) {
            console.log(err);
        })
    };
    //查询数据库中所有题型
    titleType={};
    titleTypefn=function () {
        Castle.Request.post(Config.getRequestUrl('Titletype','search'),{},function (data) {
            // console.log(data.L);
            TestVM.titleType = data.L;
        },function (err) {
            console.log(err);
        })
    };
    //选中的题目
    checkQuestion={};



}
let TestVM = avalon.define(new Test());

//编辑方案
function editways(obj) {
    let num =getRowNo(obj);
    num = num +1;
    // alert(num);
    let edittable :HTMLTableElement;
    this.edittable = document.getElementById("methodTable");
    let value = this.edittable.rows[num].cells[1].innerHTML;
    Castle.Request.post(Config.getRequestUrl('Plan','get'),{FID:value},function (data) {
        TestVM.editwaysID = data.FID;
        TestVM.editwaysName = data.Name;
        TestVM.editwaysDXNum=data.DXnumber;
        TestVM.editwaysDXScore=data.DXscore;
        TestVM.editwaysMDXNum=data.DDXnumber;
        TestVM.editwaysMDXScore=data.DDXscore;
        TestVM.editwaysPDNum=data.PDnumber;
        TestVM.editwaysPDScore=data.PDscore;
        TestVM.editwaysAllScore=data.Score;
        console.log(data);
    },function (err) {
        console.log(err);
        // alert(err);

    })
}

//更新方案
function updateways() {
    let waysData={FID:TestVM.editwaysID,Params:{Name:TestVM.editwaysName,DXnumber:TestVM.editwaysDXNum,DXscore:TestVM.editwaysDXScore,
        DDXnumber:TestVM.editwaysMDXNum, DDXscore:TestVM.editwaysMDXScore,PDnumber:TestVM.editwaysPDNum,
        PDscore:TestVM.editwaysPDScore, Score:TestVM.editwaysAllScore}};
    Castle.Request.post(Config.getRequestUrl('Plan','save'),waysData,function (data) {
        alert("更新成功");
        location.reload(this);
        // TestVM.waysallfn();
        // console.log(data);
    },function (err) {
        // console.log(err)
        alert(err);
    })
}

//删除方案
function deletways(obj) {
    let str = obj.parentNode.parentNode;
    let tbody = str.parentNode;
    let num =getRowNo(obj);
    num = num +1;
    let delettable :HTMLTableElement;
    this.delettable = document.getElementById("methodTable");
    let value = this.delettable.rows[num].cells[1].innerHTML;
    Castle.Request.post(Config.getRequestUrl('Plan','del'),{FID:value},function (data) {
        console.log(data);
    },function (err) {
        console.log(err);
    });
    tbody.removeChild(str);
}
// window.onload = function () {
//     TestVM.waysallfn();
// };

//编辑试卷模板
function editModel(obj) {
    let num =getRowNo(obj);
    num = num +1;
    // alert(num);
    let edittable :HTMLTableElement;
    this.edittable = document.getElementById("ModelTable");
    let value = this.edittable.rows[num].cells[0].innerHTML;
    TestVM.editaddSID = value;
    Castle.Request.post(Config.getRequestUrl('Exampaper','get'),{SID:value},function (data) {
        TestVM.editaddKSName=data.SJName;
        TestVM.editaddKSBatch=data.TBatch;
        TestVM.editaddKSSubject = data.KID;
        // Castle.Request.post(Config.getRequestUrl('Discipline','get'),{KID:data.KID},function (data1) {
        //     TestVM.editaddKSSubject=data.Name;
        // },function (err) {
        //     console.log(err);
        //     // alert(err);
        // });
        TestVM.editaddKSMethod=data.FID;
        TestVM.editaddKSDifficuty=data.ELID;
        TestVM.editaddKSTime=data.TTime;
        TestVM.editaddKSExamStart=data.TSTime;
        TestVM.editaddKSExamEnd=data.TETime;
        TestVM.editaddKSTrainStart=data.PSTime;
        TestVM.editaddKSTrainEnd=data.PETime;
        // console.log(data);
    },function (err) {
        // console.log(err);
        // alert(err);
    })
}
//更新试卷模板
    function updateModel() {
    let waysData={SID:TestVM.editaddSID,Params:{KID:TestVM.editaddKSSubject,UID:2,TTime:TestVM.editaddKSTime,
        SJName:TestVM.editaddKSName,TBatch:TestVM.editaddKSBatch, FID:TestVM.editaddKSMethod,
        ELID:TestVM.editaddKSDifficuty,TSTime:TestVM.editaddKSExamStart,TETime:TestVM.editaddKSExamEnd,
        PSTime:TestVM.editaddKSTrainStart,PETime:TestVM.editaddKSTrainEnd}};
    Castle.Request.post(Config.getRequestUrl('Exampaper','save'),waysData,function (data) {
        alert("更新成功");
        location.reload(this);
    },function (err) {
        alert(err);
    })
}

//删除方案
function deletModel(obj) {
    let str = obj.parentNode.parentNode;
    let tbody = str.parentNode;
    let num =getRowNo(obj);
    num = num +1;
    let delettable :HTMLTableElement;
    this.delettable = document.getElementById("ModelTable");
    let value = this.delettable.rows[num].cells[0].innerHTML;
    Castle.Request.post(Config.getRequestUrl('Exampaper','del'),{SID:value},function (data) {
        // console.log(data);
    },function (err) {
        // console.log(err);
    });
    tbody.removeChild(str);
}

//添加试题
//查找当前点击的试题
function editQuestion(obj) {
    let num =getRowNo(obj);
    num = num +1;
    // alert(num);
    let edittable :HTMLTableElement;
    this.edittable = document.getElementById("questionTable");
    let value = this.edittable.rows[num].cells[1].innerHTML;
    // alert(value);
    // TestVM.checkQuestion.push(TestVM.creattype + value);
    insRow(value);
}

//插入行及内容
function insRow(string) {
    let edittable :HTMLTableElement;
    this.edittable = document.getElementById("checkQuTable");
    let x = this.edittable.insertRow(1);
    // for (let i = 0; i < 4; i++) {
        let y = x.insertCell(0);
        let z = x.insertCell(1);
        z.innerHTML = ' <button class="btn btn-sm btn-link" onclick="deletQuestion(this)">移除</button>';
        y.innerHTML = string;
    // }
    // var z = x.insertCell(4);
    // z.innerHTML = "<button class='tableimg1' onclick='editfun(this)'><button class='tableimg2' onclick='delet(this)'>";
}
//移除试题
function deletQuestion(obj) {
    let str = obj.parentNode.parentNode;
    let tbody = str.parentNode;
    // let num =getRowNo(obj);
    // num = num +1;
    // let delettable :HTMLTableElement;
    // this.delettable = document.getElementById("ModelTable");
    // let value = this.delettable.rows[num].cells[0].innerHTML;

    tbody.removeChild(str);
}

//编辑真实试卷

function TranslateWays(id){
    // Castle.Request.post(Config.getRequestUrl('Plan','Search'),{W:{FID:id}},function (data1) {
    //     console.log(data1.L[0].Name);
    //     return data1.L[0].Name;
    //     // console.log(value);
    // },function (err) {
    //     console.log(err);
    // });
    if (id==2) return "方案1";
    else if(id==1) return "测试方案";
}
function TranslateToMthodName(name){
    if(name=="方案1") return 2;
    else if(name=="测试方案") return 1;
    else if(name == "方案2") return 3;
}
function TransToSubjectname(name){
    if (name =="语文") return 2;
    else if(name=="数学") return 5;
    else if(name == "英语") return 4;
}
